package cn.iocoder.yudao.module.system.controller.admin.roledevice;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.roledevice.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.roledevice.RoleDeviceDO;
import cn.iocoder.yudao.module.system.service.roledevice.RoleDeviceService;

@Tag(name = "管理后台 - 设备和角色关联")
@RestController
@RequestMapping("/system/role-device")
@Validated
public class RoleDeviceController {

    @Resource
    private RoleDeviceService roleDeviceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备和角色关联（支持批量和单个）")
//    @PreAuthorize("@ss.hasPermission('system:role-device:create')")
    public CommonResult<Long> createRoleDevice(@Valid @RequestBody List<RoleDeviceSaveReqVO> createReqVOList) {
        return success(roleDeviceService.createRoleDevice(createReqVOList));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-device:update')")
    public CommonResult<Boolean> updateRoleDevice(@Valid @RequestBody RoleDeviceSaveReqVO updateReqVO) {
        roleDeviceService.updateRoleDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-device:delete')")
    public CommonResult<Boolean> deleteRoleDevice(@RequestBody RoleDeviceRespVO deleteReqVO) {
        roleDeviceService.deleteRoleDevice(deleteReqVO);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备和角色关联")
//    @PreAuthorize("@ss.hasPermission('system:role-device:delete')")
    public CommonResult<Boolean> deleteRoleDeviceList(@RequestParam("ids") List<Long> ids) {
        roleDeviceService.deleteRoleDeviceListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备和角色关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('system:role-device:query')")
    public CommonResult<RoleDeviceRespVO> getRoleDevice(@RequestParam("id") Long id) {
        RoleDeviceDO roleDevice = roleDeviceService.getRoleDevice(id);
        return success(BeanUtils.toBean(roleDevice, RoleDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备和角色关联分页")
//    @PreAuthorize("@ss.hasPermission('system:role-device:query')")
    public CommonResult<PageResult<RoleDeviceRespVO>> getRoleDevicePage(@Valid RoleDevicePageReqVO pageReqVO) {
        PageResult<RoleDeviceDO> pageResult = roleDeviceService.getRoleDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoleDeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备和角色关联 Excel")
//    @PreAuthorize("@ss.hasPermission('system:role-device:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleDeviceExcel(@Valid RoleDevicePageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoleDeviceDO> list = roleDeviceService.getRoleDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备和角色关联.xls", "数据", RoleDeviceRespVO.class,
                BeanUtils.toBean(list, RoleDeviceRespVO.class));
    }
}